<?php
/**
 * Created by PhpStorm.
 * User: Thans
 * Date: 2018/1/3
 * Time: 上午10:48
 */

namespace app\common\controller;

use thans\auth\facade\Token;
use think\Controller;
use think\exception\HttpResponseException;
use think\facade\Request;
use think\facade\Response;

class Base extends Controller
{
    protected $token;
    protected $deviceType;
    protected $userType;
    protected $uid;
    protected $uinfo;
    protected $isAjax;

    public function __construct()
    {
        parent::__construct();
        $this->isAjax = Request::isAjax();
        $this->init();
    }

    public function init()
    {
        hook_one('cmf_init'); //HOOK:cmf初始化
    }


    public function results($data, array $header = [], $type = 'json')
    {
        $data['time'] = time();
        $type = $type ?: $this->getResponseType();
        $response = Response::create($data, $type)->header($header);
        throw new HttpResponseException($response);
    }

    public function isLogin()
    {
        $token = Token::instance();
        $res = $token->isLogin();
        if ($res) {
            $this->uinfo = $token->getUinfo();
            $this->userType = $this->uinfo['user_type'];
            $this->token = $token->token;
            $this->deviceType = $token->token;
            $this->uid = $token->getUid();
            return $res;
        }
        return false;
    }

    /** 后台权限验证 **/
    public function adminAuth()
    {
        //验证是否为后台用户
        if (!$this->uinfo || $this->userType != 1) {
            return false;
        }
        //权限验证
        $auth = \thans\auth\facade\Auth::instance();
        $group = $auth->getGroups($this->uid);
        if (!$group) {
            return false;
        }
        //超级管理员不认证
        if ($group['group_id'] == 1) {
            return true;
        }
        $res = $auth->check(request()->module() . '/' . request()->controller() . '/' . request()->action(), $this->uid);
        return $res;
    }

    public function fetch($template = '', $vars = [], $config = [])
    {
        $new = [];
        $old = config('template.tpl_replace_string');
        $option = cmf_option('system');
        if ($option['cdn_status']) {
            $new = [
                '__STATIC__' => $option['cdn_url'] . $old['__STATIC__'],
                '__LAYUI__' => $option['cdn_url'] . $old['__LAYUI__'],
                '__TMPL__' => $option['cdn_url'] . '/theme/' . request()->module(),
            ];
        } else {
            $new['__TMPL__'] = '/theme/' . request()->module();
        }
        $view_path = BASE_ROOT . 'public/theme/' . request()->module() . '/';
        $this->view->config('view_path', $view_path);
        $this->view->config('tpl_replace_string', array_merge($old, $new));
        $dir = explode('.', request()->controller());
        if ($template == '') {
            $template = request()->action();
        }
        $template = $view_path . $dir[1] . '/' . $template . '.html';
        return $this->view->fetch($template, $vars, $config);
    }
}
